package leetcode.每日一题;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2022/1/5 12:46
 * @description：https://leetcode-cn.com/problems/replace-all-s-to-avoid-consecutive-repeating-characters/
 */
public class 替换所有的问号 {
    @Test
    public void test() {
        Solution solution = new Solution();
        System.out.println(solution.modifyString("??yw?ipkj?"));
    }

    class Solution {
        public String modifyString(String s) {
            StringBuilder builder = new StringBuilder();
            char pre = ' ';
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                if (c == '?') {
                    for (int j = 0; j < 26; j++) {
                        char cur = (char) (j + 'a');
                        if (cur == pre) continue;
                        if (i + 1 < s.length() && cur == s.charAt(i + 1)) {
                            continue;
                        } else {
                            builder.append(cur);
                            pre = cur;
                            break;
                        }
                    }
                } else {
                    builder.append(c);
                    pre = c;
                }
            }
            return builder.toString();
        }
    }
}
